//
//  ViewController.m
//  Bluetooth Chat App
//
//  Created by iD Student on 7/15/13.
//  Copyright (c) 2013 ZTBWorks. All rights reserved.
//
/*
#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *text;

@end

@implementation ViewController
@synthesize text;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
   fonts = [[NSArray alloc]initWithObjects:
             [UIFont fontWithName:@"digitalanarchy-Regular.ttf" size:17],
             [UIFont fontWithName:@"DIMIS__.ttf" size:17],
             [UIFont fontWithName:@"DIMITRI_.ttf" size:17],
             [UIFont fontWithName:@"lunasol aurora.ttf" size:17],
             [UIFont fontWithName:@"lunasol eclipse.ttf" size:17],
             [UIFont fontWithName:@"TechnoHideo.ttf" size:17],
             [UIFont fontWithName:@"TECHNOID.ttf" size:17],
             [UIFont fontWithName:@"Tr2n.ttf" size:17],
             [UIFont fontWithName:@"SquaredDisplay.ttf" size:17],
             nil];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    for (int i = 0; i < fonts.count; i ++) {
        [self pickerView:pickerView titleForRow:i forComponent:1];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return fonts.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSLog([[fonts objectAtIndex:(row - 1)] fontName]);
    return [[[[fonts objectAtIndex:row] fontName] componentsSeparatedByString:@"."] objectAtIndex:0];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    text.font = [fonts objectAtIndex:row - 1];
}

- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *tview = (UILabel *)view;
    if(!tview)
    {
        tview = [[UILabel alloc]init];
        tview.font = [fonts objectAtIndex:row - 1];
        tview.text = [[[[fonts objectAtIndex:row] fontName] componentsSeparatedByString:@"."] objectAtIndex:0];
    }
    return tview;
}

@end
*/

#import "ViewController.h"
#import <GameKit/GameKit.h>

@implementation ViewController

@synthesize txtMessage;
@synthesize connect;
@synthesize usernameField;
@synthesize userNameLabel;
@synthesize viewController;

GKPeerPickerController *picker;

-(AppDelegate *) AppDelegate{
    
    return [UIApplication sharedApplication].delegate;
}

-(IBAction) btnConnect:(id) sender {
    if ([usernameField.text isEqualToString:@""]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Username not entered" message:@"Please enter a username and then click connect again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    } else {
        [self AppDelegate].username = usernameField.text;
        NSLog([self AppDelegate].username);
        picker = [[GKPeerPickerController alloc] init];
        picker.delegate = self;
        picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
        [picker show];
    }
}

- (void)peerPickerController:(GKPeerPickerController *)picker
              didConnectPeer:(NSString *)peerID
                   toSession:(GKSession *) session {
    [self.appDelegate.arrayOfSessions addObject:session];
    session.delegate = self;
    [session setDataReceiveHandler:self withContext:nil];
    picker.delegate = nil;
    
    [picker dismiss];
}

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
    picker.delegate = nil;
    
    [connect setHidden:NO];
}



- (void)session:(GKSession *)session
           peer:(NSString *)peerID
 didChangeState:(GKPeerConnectionState)state {
    switch (state)
    {
        case GKPeerStateConnected: {
            NSLog(@"connected");
            if([[UIScreen mainScreen] bounds].size.height == 568) { viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChatViewTall"]; }
            else if([[UIScreen mainScreen] bounds].size.height == 480 || [[UIScreen mainScreen] bounds].size.height == 960) { viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChatView"]; }
            [self presentViewController:viewController animated:YES completion:nil];
        }
            break;
        case GKPeerStateDisconnected:
            NSLog(@"disconnected");
            for (int i = 0; i < self.appDelegate.arrayOfSessions.count; i++) {
                GKSession *endSession = [self.appDelegate.arrayOfSessions objectAtIndex:i];
                endSession = nil;
            }
            
            [connect setHidden:NO];
            break;
        case GKPeerStateAvailable:
        case GKPeerStateConnecting:
        case GKPeerStateUnavailable:
            break;
    }
}

- (void) mySendDataToPeers:(NSData *) data
{
    if (((AppDelegate *)[[UIApplication sharedApplication] delegate]).currentSession)
        [((AppDelegate *)[[UIApplication sharedApplication] delegate]).currentSession sendDataToAllPeers:data
                                   withDataMode:GKSendDataReliable
                                          error:nil];
}

- (void) receiveData:(NSData *)data
            fromPeer:(NSString *)peer
           inSession:(GKSession *)session
             context:(void *)context {
    
    //---convert the NSData to NSString---
    NSLog(@"data recieved");
    [viewController receiveData:data fromPeer:peer inSession:session context:context];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}



#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [connect setHidden:NO];
    [super viewDidLoad];
    usernameField.delegate = self;
}

- (void)viewDidUnload
{
    [self setUserNameLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        usernameField.center = CGPointMake(usernameField.center.x, usernameField.center.y - 50);
        connect.center = CGPointMake(connect.center.x, connect.center.y - 50);
        userNameLabel.center = CGPointMake(userNameLabel.center.x, userNameLabel.center.y - 50);
    }];
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        usernameField.center = CGPointMake(usernameField.center.x, usernameField.center.y + 50);
        connect.center = CGPointMake(connect.center.x, connect.center.y + 50);
        userNameLabel.center = CGPointMake(userNameLabel.center.x, userNameLabel.center.y + 50);
    }];
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    
    [usernameField resignFirstResponder];
    return YES;
}


@end